#!/usr/bin/env python
# coding=utf-8
# version 1.0.0.1

__author__ = 'vavava'

from configparser import ConfigParser

__version__ = "1.0.0.1"

class GripperConfig(object):
    def __init__(self,fullFileName="config.conf"):
        self.fileName=fullFileName
        self.parser = ConfigParser()
        #version
        self.version         = __version__
        #db
        self.host            =  "localhost"
        self.port            =  3306
        self.db              =  "wr_trace"
        self.user            =  "root"
        self.passwd          =  "root"
        #config
        self.log_config_file =  "./logger.conf"
        self.logger          =  "gripper"
        #runtime
        self.debug_level     =  1
        self.sock_timeout    =  30.0        # s
        self.chk_period      =  3600000.0   # ms
        self.work_queue_size =  10

    def load(self,fullFileName):
        self.parser.read(fullFileName)
        print(self.parser.sections())
        try:
            version         =  self.parser.get("version","version")
            host            =  self.parser.get("db","host")
            port            =  self.parser.getint("db","port")
            db              =  self.parser.get("db","db")
            user            =  self.parser.get("db","user")
            passwd          =  self.parser.get("db","passwd")
            log_config_file =  self.parser.get("log","log_config_file")
            logger          =  self.parser.get("log","logger")
            debug_level     =  self.parser.getint("runtime","debug_level")
            sock_timeout    =  self.parser.getfloat("runtime","sock_timeout")
            chk_period      =  self.parser.getfloat("runtime","chk_period")
            work_queue_size =  self.parser.getint("runtime","work_queue_size")
        except Exception as e:
            print(e)

        if  version          != "" : self.version         = version
        if  host             != "" : self.host            = host
        if  port                   : self.port            = port
        if  db               != "" : self.db              = db
        if  user             != "" : self.user            = user
        if  passwd           != "" : self.passwd          = passwd
        if  log_config_file  != "" : self.log_config_file = log_config_file
        if  logger           != "" : self.logger          = logger
        if  debug_level            : self.debug_level     = debug_level
        if  sock_timeout           : self.sock_timeout    = sock_timeout
        if  chk_period             : self.chk_period      = chk_period
        if  work_queue_size        : self.work_queue_size = work_queue_size

        if version != __version__ :
            raise Exception("config file not match, need %s, got %s"%
                            (__version__, version))

if __name__ == "__main__":
    gc = GripperConfig()
    gc.load("tracker.conf")
    print(gc.chk_period)
